//
//  PhotosView.m
//  weibo
//
//  Created by sangcixiang on 17/1/4.
//  Copyright © 2017年 sangcixiang. All rights reserved.
//

#import "PhotosView.h"
#import "SDPhotoBrowser.h"

#define PhotoMaxCol(count) ((count==4)?2:3)
#define ImageViewWH (ScreenWidth - 30) / 3
#define Margin 5

@interface PhotosView()<SDPhotoBrowserDelegate>

@end

@implementation PhotosView


- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        
    }
    return self;
}



-(void)setPhotos:(NSArray *)photos{
    _photos = photos;
    
    
    NSUInteger count = photos.count;
    
    while (self.subviews.count < count) {
        ImageView *imageView = [[ImageView alloc]init];
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapImageView:)];
        [imageView addGestureRecognizer:tap];
        [self addSubview:imageView];
    }
    
    for (UIView *view in self.subviews) {
        view.hidden = YES;
    }
    
    for (int i = 0; i < count; i++) {
        ImageView *imageView = self.subviews[i];
        imageView.tag = i;
        imageView.imageUrl = photos[i];
        imageView.hidden = NO;
    }
}
-(void)tapImageView:(UITapGestureRecognizer *)tap{
    UIView *imageView = tap.view;
    SDPhotoBrowser *browser = [[SDPhotoBrowser alloc]init];
    browser.currentImageIndex = imageView.tag;
    browser.sourceImagesContainerView = self;
    browser.imageCount = self.photos.count;
    browser.delegate = self;
    [browser show];
}
- (UIImage *)photoBrowser:(SDPhotoBrowser *)browser placeholderImageForIndex:(NSInteger)index{
    ImageView *imageView = self.subviews[index];
    return imageView.image;
}
- (NSURL *)photoBrowser:(SDPhotoBrowser *)browser highQualityImageURLForIndex:(NSInteger)index{
    return [NSURL URLWithString:self.photos[index]];
}
-(void)layoutSubviews{
    
    [super layoutSubviews];
    NSUInteger imageCount = self.photos.count;
    int maxCol = PhotoMaxCol(imageCount);
    for (int i = 0; i < imageCount; i++) {
        ImageView *imageView = self.subviews[i];
        int col = i % maxCol;
        imageView.x = col * (ImageViewWH + Margin);
        int row = i / maxCol;
        imageView.y = row * (ImageViewWH + Margin);
        imageView.width = ImageViewWH;
        imageView.height = ImageViewWH;
    }
}
+(CGSize)sizeWithCont:(NSInteger)count{
    
    int maxCol = PhotoMaxCol(count);
    NSInteger cols = (count >= maxCol)?maxCol : count;
    CGFloat photosW = cols * ImageViewWH + (count - 1) * Margin;
    
    NSInteger rows = (count + maxCol - 1) / maxCol;
    CGFloat photosH = rows * ImageViewWH + (rows - 1) * Margin ;
    return CGSizeMake(photosW, photosH);
}
@end
@implementation ImageView

- (instancetype)init{
    self = [super init];
    if (self) {
        self.userInteractionEnabled = YES;
        self.contentMode = UIViewContentModeScaleAspectFill;
        self.clipsToBounds = YES;
        self.backgroundColor = HexRGB(0xeeeeee);
    }
    return self;
}

-(void)setImageUrl:(NSString *)imageUrl{
    _imageUrl = imageUrl;
    [self sd_setImageWithURL:[NSURL URLWithString:imageUrl]];
}

@end



